<?php
namespace app\shops\controller;

use app\plug\controller\qiniu\Qiniu;

use think\Db;
use think\Model;


/**
 * 文章管理
 */
class Article extends Base
{
    /**
     * @return mixed
     * yang yang
     * 文章列表
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 查询数据
        $model = Db::name('article');
        $list = $model->where('is_del',0)->where('shop_id',$this->shop_id)->order("id desc")->paginate($pageSize);

        $this->assign('total',$list->total());
        $this->assign('page',$list->render());
        $this->assign('list',$list);

        return $this->fetch();
    }

    /**
     * yang yang
     * @return mixed
     * 添加文章
     */
    public function add_article()
    {
        if(request()->isPost()){

            $model = Db::name('article');
            // 存入数据库
            $data['sort']      = input("sort");
            $status = Db::name('article_sort')->where('sort',$data['sort'] )->field('status,id')->find();
            $data['status'] = 0;
            $data['sort_id'] =  $status['id'];
            $data['sort_status'] = $status['status'];
            $data['title']    = input("title");
            $data['author']    = input("author");
            $data['edit']    = 0;
            $data['content']    = input("content");
            $data['shop_id']     = $this->shop_id;
            $data['create_time'] = time();
            $ret =$model->insert($data);
            if($ret){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }

        }
        $sort = Db::name('article_sort')->where('is_del',0)->select();
        $this->assign('sort',$sort);
        $this->assign('author',$this->shop_user_name);
        return $this->fetch();
    }

    /**
     * yang yang
     * @return mixed
     * 编辑文章
     */
    public function edit_article()
    {
        $id = input('id');

        $model = Db::name('article');;

        // 更新数据
        if(request()->isPost()){

            $data['sort'] = input("sort");
            $data['title'] = input("title");
            $data['edit']    = 1;
            $data['author'] = input("author");
            $data['content'] = input("content");
            $data['status'] = 0;
            $status = Db::name('article_sort')->where('sort',$data['sort'] )->field('status,id')->find();
            $data['sort_id'] =  $status['id'];
            $data['sort_status'] = $status['status'];
            if(count($data)<=0){
                $this->error("未做任何修改");
            }

            $re = $model->where('id',$id)->update($data);
            if($re){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }
        }

        // 详情
        $info = $model->where('id',$id)->find();
        $this->assign('info',$info);
        $sort = Db::name('article_sort')->where('is_del',0)->select();
        $this->assign('sort',$sort);

        return $this->fetch();
    }

    /**
     * yang yang
     * 删除文章
     */
    public function article_del()
    {
        $id = input('id');
        if (request()->isAjax()) {

            $res = Db::name('article')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }
    
}
